#ifndef IMAGEWORKER_H
#define IMAGEWORKER_H


#include <QObject>
#include <QImage>
#include <QThread>
#include <QMutex>
#include <opencv2/opencv.hpp>
#include <qDebug>
#include "detector.h"
#include "settings.h"

class imageWorker : public QObject {
    Q_OBJECT
public:
    explicit imageWorker(DetectionSettings &d, ResultSettings &r,QObject *parent = nullptr) : detectionSettings(d) , resultSettings(r), QObject(parent) {}

    void startCapture(int cameraIndex = 0) {
        cap.open(cameraIndex);
    }

    void stopCapture() {
        cap.release();
    }

public slots:
    void processImage() {
        while (cap.isOpened()) {
            cv::Mat frame;
            cap >> frame;
            if (frame.empty()) {
                QThread::msleep(10);
                continue;
            }

            resultSettings.isHaveSpaker = false;
            resultSettings.isHavePassener = false;
            resultSettings.isHaveIntruder = false;

            if (detectionSettings.isDetectOn) {
                // 检测火花
                if (detectionSettings.isDetectSpakerOn) {
                    auto regions = sparkDetector.detect(frame);
                    resultSettings.isHaveSpaker = !regions.empty();
                    frame = sparkDetector.annotateImage(frame, regions);
                }

                if (detectionSettings.isDetectPassenerOn) {
                    auto regions = passenerDetector.detect(frame);
                    //resultSettings.isHavePassener = !regions.empty();
                    frame = passenerDetector.annotateImage(frame, regions);

                    if (regions.size() < passenerDetector.facesCnt) {
                        passenerDetector.lostCnt++;
                        if (passenerDetector.lostCnt > 10)  passenerDetector.facesCnt = regions.size();
                    } else if (regions.size() > passenerDetector.facesCnt) {
                        if (passenerDetector.lostCnt == 0) resultSettings.isHavePassener = true;
                        passenerDetector.lostCnt = 0;
                        passenerDetector.facesCnt = regions.size();
                    } else {
                        passenerDetector.lostCnt = 0;
                    }
                }

                if (detectionSettings.isDetectIntruderOn) {
                    auto regions = intruderDetector.detect(frame);
                    resultSettings.isHaveIntruder = !regions.empty();
                    frame = intruderDetector.annotateImage(frame, regions);
                }
            }
            //qDebug() << resultSettings.isHaveSpaker << " " << resultSettings.isHavePassener << " " << resultSettings.isHaveIntruder;

            if (detectionSettings.isCameraOn) {
                // 转成 QImage 发射信号
                QImage img = matToQImage(frame);
                emit frameReady(img);
            }

            // 短暂休眠以控制帧率
            QThread::msleep(30);
        }

        qDebug() << "Camera not opened, stopping loop";
    }
signals:
    void frameReady(const QImage &image);

public:

private:
    cv::VideoCapture cap;

    SparkDetector sparkDetector;
    PassenerDetector passenerDetector;
    IntruderDetector intruderDetector;

    DetectionSettings &detectionSettings;
    ResultSettings &resultSettings;

    QImage matToQImage(const cv::Mat &mat) {
        if (mat.type() == CV_8UC3) {
            cv::Mat rgb;
            cv::cvtColor(mat, rgb, cv::COLOR_BGR2RGB);
            return QImage((const unsigned char*)rgb.data, rgb.cols, rgb.rows,
                          rgb.step, QImage::Format_RGB888).copy();
        } else if (mat.type() == CV_8UC1) {
            return QImage((const unsigned char*)mat.data, mat.cols, mat.rows,
                          mat.step, QImage::Format_Grayscale8).copy();
        }
        return QImage();
    }
};


#endif // IMAGEWORKER_H
